package androidx.test.espresso.base;

import android.os.Handler;
import android.os.Looper;
import android.os.MessageQueue;
import androidx.test.espresso.IdlingResource;
import androidx.test.espresso.base.Interrogator;
import java.util.concurrent.ConcurrentHashMap;

/* loaded from: classes.dex */
class LooperIdlingResourceInterrogationHandler implements IdlingResource, Interrogator.InterrogationHandler<Void> {
    private static final ConcurrentHashMap<String, LooperIdlingResourceInterrogationHandler> insts = new ConcurrentHashMap<>();
    private final String name;
    private final Interrogator.QueueInterrogationHandler<Boolean> queueHasNewTasks = new Interrogator.QueueInterrogationHandler<Boolean>(this) { // from class: androidx.test.espresso.base.LooperIdlingResourceInterrogationHandler.1
        private Boolean hasTasks = Boolean.FALSE;

        /* JADX WARN: Can't rename method to resolve collision */
        @Override // androidx.test.espresso.base.Interrogator.QueueInterrogationHandler
        public Boolean get() {
            return this.hasTasks;
        }

        @Override // androidx.test.espresso.base.Interrogator.QueueInterrogationHandler
        public boolean queueEmpty() {
            this.hasTasks = Boolean.FALSE;
            return false;
        }

        @Override // androidx.test.espresso.base.Interrogator.QueueInterrogationHandler
        public boolean taskDueLong() {
            this.hasTasks = Boolean.FALSE;
            return false;
        }

        @Override // androidx.test.espresso.base.Interrogator.QueueInterrogationHandler
        public boolean taskDueSoon() {
            this.hasTasks = Boolean.TRUE;
            return false;
        }

        @Override // androidx.test.espresso.base.Interrogator.QueueInterrogationHandler
        public boolean barrierUp() {
            this.hasTasks = Boolean.TRUE;
            return false;
        }
    };
    private volatile boolean started = false;
    private volatile MessageQueue queue = null;
    private volatile boolean idle = true;
    private volatile IdlingResource.ResourceCallback cb = null;

    @Override // androidx.test.espresso.base.Interrogator.QueueInterrogationHandler
    public Void get() {
        return null;
    }

    private LooperIdlingResourceInterrogationHandler(String name) {
        this.name = name;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static LooperIdlingResourceInterrogationHandler forLooper(Looper l) {
        String format = String.format("LooperIdlingResource-%s-%s", Long.valueOf(l.getThread().getId()), l.getThread().getName());
        LooperIdlingResourceInterrogationHandler looperIdlingResourceInterrogationHandler = new LooperIdlingResourceInterrogationHandler(format);
        LooperIdlingResourceInterrogationHandler putIfAbsent = insts.putIfAbsent(format, looperIdlingResourceInterrogationHandler);
        if (putIfAbsent != null) {
            return putIfAbsent;
        }
        new Handler(l).post(new Runnable() { // from class: androidx.test.espresso.base.LooperIdlingResourceInterrogationHandler.2
            @Override // java.lang.Runnable
            public void run() {
                LooperIdlingResourceInterrogationHandler.this.queue = Looper.myQueue();
                LooperIdlingResourceInterrogationHandler.this.started = true;
                Interrogator.loopAndInterrogate(LooperIdlingResourceInterrogationHandler.this);
            }
        });
        return looperIdlingResourceInterrogationHandler;
    }

    @Override // androidx.test.espresso.base.Interrogator.InterrogationHandler
    public void quitting() {
        transitionToIdle();
    }

    @Override // androidx.test.espresso.base.Interrogator.QueueInterrogationHandler
    public boolean queueEmpty() {
        transitionToIdle();
        return true;
    }

    @Override // androidx.test.espresso.base.Interrogator.QueueInterrogationHandler
    public boolean taskDueLong() {
        transitionToIdle();
        return true;
    }

    @Override // androidx.test.espresso.base.Interrogator.InterrogationHandler
    public boolean beforeTaskDispatch() {
        this.idle = false;
        return true;
    }

    @Override // androidx.test.espresso.base.Interrogator.QueueInterrogationHandler
    public boolean taskDueSoon() {
        this.idle = false;
        return true;
    }

    @Override // androidx.test.espresso.base.Interrogator.QueueInterrogationHandler
    public boolean barrierUp() {
        this.idle = false;
        return true;
    }

    @Override // androidx.test.espresso.IdlingResource
    public boolean isIdleNow() {
        if (this.started && this.idle) {
            return Boolean.FALSE.equals(Interrogator.peekAtQueueState(this.queue, this.queueHasNewTasks));
        }
        return false;
    }

    @Override // androidx.test.espresso.IdlingResource
    public String getName() {
        return this.name;
    }

    @Override // androidx.test.espresso.IdlingResource
    public void registerIdleTransitionCallback(IdlingResource.ResourceCallback cb) {
        this.cb = cb;
    }

    private void transitionToIdle() {
        this.idle = true;
        if (this.cb != null) {
            this.cb.onTransitionToIdle();
        }
    }
}
